package com.huasoft.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.huasoft.domain.User;
import com.huasoft.servlet.file.AddFileServlet;
import com.huasoft.servlet.file.DeleteFileServlet;
import com.huasoft.servlet.file.GiveFileServlet;
import com.huasoft.servlet.file.IsAgreeFileServlet;
import com.huasoft.servlet.file.ListFileServlet;
import com.huasoft.servlet.file.ListUserFileServlet;
import com.huasoft.servlet.file.NeedModifiedFile;
import com.huasoft.servlet.file.StatisticsFilesServlet;
import com.huasoft.servlet.file.UpdateFileServlet;
import com.huasoft.servlet.user.AddUserServlet;
import com.huasoft.servlet.user.DeleteUserServlet;
import com.huasoft.servlet.user.ListUserServlet;
import com.huasoft.servlet.user.NeedModifiedUser;
import com.huasoft.servlet.user.UpdateUserServlet;
import com.huasoft.servlet.userself.BackFileServlet;
import com.huasoft.servlet.userself.ListUserFileByUserIdServlet;
import com.huasoft.servlet.userself.ModifyPWDServlet;

public class MyServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("gbk");
		resp.setContentType("text/html;charset=gbk");
		System.out.println(req.getRequestURI());
		String uri = req.getRequestURI();
		String path = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
		System.out.println(path);
		HttpSession session = req.getSession();
		if ("/login".equals(path)) {
			LoginServlet login = new LoginServlet();
			login.execute(req, resp);
		} else {
			User user = (User) session.getAttribute("user");
			if (user == null) {
				req.setAttribute("error", "���ȵ�¼  请先登录");
				req.getRequestDispatcher("/login.jsp").forward(req, resp);
				} else {
					if ("/addUser".equals(path)) {
						new AddUserServlet().execute(req, resp);
					} else if ("/loginOut".equals(path)) {
						new LogoutServlet().execute(req, resp);
					} else if ("/listUser".equals(path)) {
						new ListUserServlet().execute(req, resp);
					} else if ("/needModifiedUser".equals(path)) {
						new NeedModifiedUser().execute(req, resp);
					} else if ("/updateUser".equals(path)) {
						new UpdateUserServlet().execute(req, resp);
					} else if ("/deleteUser".equals(path)) {
						new DeleteUserServlet().execute(req, resp);
					} else if ("/addFile".equals(path)) {
						new AddFileServlet().exexute(req, resp);
					} else if ("/listFile".equals(path)) {
						new ListFileServlet().execute(req, resp);
					} else if ("/needModifiedFile".equals(path)) {
						new NeedModifiedFile().execute(req, resp);
					} else if ("/updateFile".equals(path)) {
						new UpdateFileServlet().execute(req, resp);
					} else if ("/deleteFile".equals(path)) {
						new DeleteFileServlet().execute(req, resp);
					} else if ("/giveFile".equals(path)) {
						// 借阅案卷
						new GiveFileServlet().execute(req, resp);
					} else if ("/listUserFile".equals(path)) {
						// 部门经理需要审批的案卷
						new ListUserFileServlet().execute(req, resp);
					} else if ("/isAgreeFile".equals(path)) {
						// 部门经理审批
						new IsAgreeFileServlet().execute(req, resp);
					} else if ("/modifyPWD".equals(path)) {
						// 修改密码
						new ModifyPWDServlet().execute(req, resp);
					} else if ("/listUserFileByUserId".equals(path)) {
						new ListUserFileByUserIdServlet().execute(req, resp);
					} else if ("/backFile".equals(path)) {
						//归还案卷
						new BackFileServlet().execute(req, resp);
					}else if("/statisticsFiles".equals(path)){
						System.out.println("bbbbbbbb");
						new StatisticsFilesServlet().execute(req,resp);
					}
				}
			}

		}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
